package com.tencreat.common.config;

import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.tencreat.common.core.domain.entity.SysDictData;
import com.tencreat.common.utils.DictUtils;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author huang wei
 * @version 1.0
 * @date 2021/9/2 10:34
 */
@Configuration
@ConditionalOnClass({WxPayService.class})
//@AllArgsConstructor
public class WxPayConfiguration {

    @Bean("wxService")
//    @ConditionalOnMissingBean
    public WxPayService wxService() {
//        List<SysDictData> wxPayConfig = DictUtils.getDictCache("wx_pay_configH5");
        List<SysDictData> wxPayConfig = DictUtils.getDictCache("wx_pay_config");
        WxPayProperties properties=new WxPayProperties();
        try {
            assert wxPayConfig != null;
            for (SysDictData i : wxPayConfig) {
                if("appId".equals(i.getDictLabel())){
                    properties.setAppId(i.getDictValue());
                }
                if("mchKey".equals(i.getDictLabel())){
                    properties.setMchKey(i.getDictValue());
                }
                if("mchId".equals(i.getDictLabel())){
                    properties.setMchId(i.getDictValue());
                }
                if("notifyUrl".equals(i.getDictLabel())){
                    properties.setNotifyUrl(i.getDictValue());
                }
            }
        }catch (Exception e){
            return new WxPayServiceImpl();
        }

        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(StringUtils.trimToNull(properties.getAppId()));
        payConfig.setMchId(StringUtils.trimToNull(properties.getMchId()));
        payConfig.setMchKey(StringUtils.trimToNull(properties.getMchKey()));
        payConfig.setSubAppId(StringUtils.trimToNull(properties.getSubAppId()));
        payConfig.setSubMchId(StringUtils.trimToNull(properties.getSubMchId()));
        payConfig.setKeyPath(StringUtils.trimToNull(properties.getKeyPath()));
        payConfig.setNotifyUrl(StringUtils.trimToNull(properties.getNotifyUrl()));
        // 可以指定是否使用沙箱环境
        payConfig.setUseSandboxEnv(false);

        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }


    @Bean("wxServiceH5")
//    @ConditionalOnMissingBean
    public WxPayService wxServiceH5() {
        List<SysDictData> wxPayConfig = DictUtils.getDictCache("wx_pay_configH5");
//        List<SysDictData> wxPayConfig = DictUtils.getDictCache("wx_pay_config");
        WxPayProperties properties=new WxPayProperties();
        try {
            assert wxPayConfig != null;
            for (SysDictData i : wxPayConfig) {
                if("appId".equals(i.getDictLabel())){
                    properties.setAppId(i.getDictValue());
                }
                if("mchKey".equals(i.getDictLabel())){
                    properties.setMchKey(i.getDictValue());
                }
                if("mchId".equals(i.getDictLabel())){
                    properties.setMchId(i.getDictValue());
                }
                if("notifyUrl".equals(i.getDictLabel())){
                    properties.setNotifyUrl(i.getDictValue());
                }
            }
        }catch (Exception e){
            return new WxPayServiceImpl();
        }

        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(StringUtils.trimToNull(properties.getAppId()));
        payConfig.setMchId(StringUtils.trimToNull(properties.getMchId()));
        payConfig.setMchKey(StringUtils.trimToNull(properties.getMchKey()));
        payConfig.setSubAppId(StringUtils.trimToNull(properties.getSubAppId()));
        payConfig.setSubMchId(StringUtils.trimToNull(properties.getSubMchId()));
        payConfig.setKeyPath(StringUtils.trimToNull(properties.getKeyPath()));
        payConfig.setNotifyUrl(StringUtils.trimToNull(properties.getNotifyUrl()));
        // 可以指定是否使用沙箱环境
        payConfig.setUseSandboxEnv(false);

        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }

}
